package com.yungu.swift.admin.route.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.google.common.collect.Lists;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.admin.AdminParam;
import com.yungu.swift.base.model.admin.AdminUuidParam;
import com.yungu.swift.system.sys.model.dto.SysResourceDto;
import com.yungu.swift.system.sys.model.dto.SysRoleResourceDto;
import com.yungu.swift.system.sys.model.param.AdminResourceParam;
import com.yungu.swift.system.sys.service.SysResourceService;
import com.yungu.swift.system.sys.service.SysRoleResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author : cuixiuyin
 * @date : 2019/6/13
 */
@RestController
@RequestMapping("/admin/v1/system/permission/resource")
@Api(value = "【系统服务-权限设置中心】", tags = "资源操作相关接口")
public class ResourceAdmin {

    @Reference
    private SysResourceService sysResourceService;
    @Reference
    private SysRoleResourceService sysRoleResourceService;


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "资源信息列表", httpMethod = "GET")
    public ResponseData<List<SysResourceDto>> list(@RequestBody AdminParam adminParam) {
        ResponseData<List<SysResourceDto>> responseData = sysResourceService.queryAllResource(adminParam);
        if (responseData.isSuccess() && CollectionUtils.isNotEmpty(responseData.getData())) {
            return ResponseData.buildSuccessResponse(buildAuthorityTree(responseData.getData(), "0"));
        }
        return ResponseData.buildSuccessResponse(Lists.newArrayList());
    }


    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "资源信息新增或更新", httpMethod = "POST")
    public ResponseData<Boolean> add(@RequestBody AdminResourceParam resourceParam) {
        return sysResourceService.saveOrUpdate(resourceParam);
    }

    @RequestMapping("/del")
    @ApiOperation(value = "资源信息删除", httpMethod = "POST")
    public ResponseData<Boolean> del(@RequestBody AdminUuidParam uuidParam) {
        //删除 roleResource 关联
        SysRoleResourceDto paramDto = new SysRoleResourceDto();
        paramDto.setResourceUuid(uuidParam.getUuid());
        ResponseData<Boolean> delRoleResource = sysRoleResourceService.del(paramDto);
        ResponseData<Boolean> del = sysResourceService.del(uuidParam.getUuid(), SysResourceDto.class);
        return ResponseData.buildSuccessResponse(delRoleResource.isSuccess() && del.isSuccess());
    }


    /**
     * 构建菜单资源树
     *
     * @param resourceList 菜单资源列表
     * @param parentId     父级id
     * @return List<SysResourceDto>
     */
    private List<SysResourceDto> buildAuthorityTree(List<SysResourceDto> resourceList, String parentId) {
        List<SysResourceDto> buildRes = new ArrayList<>();
        for (final SysResourceDto sysResourceDto : resourceList) {
            if (sysResourceDto != null) {
                if (parentId.equals(sysResourceDto.getResourceParent())) {
                    // 获取该资源下的所有子资源
                    List<SysResourceDto> subResources = buildAuthorityTree(resourceList, sysResourceDto.getUuid());
                    subResources.stream().forEach(subResource -> subResource.setResourceParentName(sysResourceDto.getResourceName()));
                    sysResourceDto.setSysResourceDtoList(subResources);
                    buildRes.add(sysResourceDto);
                }
            }
        }
        buildRes.sort(Comparator.comparingInt(SysResourceDto::getResourceSort));
        return buildRes;
    }

}
